package com.shixi.controller;

import com.github.pagehelper.PageInfo;
import com.shixi.domain.MyOrder;
import com.shixi.domain.Order;
import com.shixi.domain.User;
import com.shixi.service.OrderService;
import com.shixi.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class OrderController {

    @Autowired
    OrderService orderService;

    @RequestMapping("/orderInfo")
    public String toOrderInfo(String id, String price, Model model, HttpServletRequest request) {
        model.addAttribute("id", id);
        model.addAttribute("price", price);
        User user = (User) request.getSession().getAttribute("loginUser");
        model.addAttribute("username", user.getUsername());
        return "order/orderInfo";
    }

    @RequestMapping("/orderSubmit")
    @ResponseBody
    public Map<String, Object> orderSubmit(String goodsid, String username, String address, String price) {
        String id = TimeUtil.makeOrderId(Integer.parseInt(goodsid));
        String time = TimeUtil.getOrderTime();
        System.out.println(goodsid+username+address+price);
        Order order = new Order(id, username, Integer.parseInt(goodsid), time, address, Integer.parseInt(price));
        System.out.println(order);
        boolean result = orderService.addOrder(order);
        HashMap<String, Object> map = new HashMap<>();
        if (result == true) {
            map.put("code", 0);
        } else {
            map.put("code", -1);
        }
        return map;
    }

    @RequestMapping("/myorder")
    public String tomyorder() {
        return "order/myorder";
    }

    @RequestMapping("/mysellorder")
    public String tomysellorder() {
        return "order/mysellorder";
    }

    @RequestMapping("/getUserOrder")
    @ResponseBody
    public Map<String, Object> getUserOrder(@RequestParam("page") Integer page,
                                            @RequestParam("limit") Integer limit,
                                            HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        User user = (User) request.getSession().getAttribute("loginUser");
        String username = user.getUsername();
        List<MyOrder> orderList = orderService.getUserOrder(page, limit, username);
        PageInfo<MyOrder> pageInfo = new PageInfo<>(orderList);
        map.put("code", 0);
        map.put("msg", "操作成功");
        map.put("count", pageInfo.getTotal());
        map.put("data", pageInfo.getList());
        return map;
    }

    @RequestMapping("/getUserOrderByUsername")
    @ResponseBody
    public Map<String, Object> getUserOrderByUsername(@RequestParam("page") Integer page,
                                                      @RequestParam("limit") Integer limit,
                                                      HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        User user = (User) request.getSession().getAttribute("loginUser");
        String username = user.getUsername();
        List<MyOrder> orderList = orderService.getUserOrderByUsername(page, limit, username);
        PageInfo<MyOrder> pageInfo = new PageInfo<>(orderList);
        map.put("code", 0);
        map.put("msg", "操作成功");
        map.put("count", pageInfo.getTotal());
        map.put("data", pageInfo.getList());
        return map;
    }

}
